$(function(){

    var currHeader = new Vue({
        el:'#currency-nav',
        data:{
            currYear: null,  //当前年
            currMinMonth: null,   //当前日历小月： 如果本周在2个月结合点
            currMaxMonth: null,   //当前日历大月
            month:[
                {indexId: 1, value:'1月'},
                {indexId: 2, value:'2月'},
                {indexId: 3, value:'3月'},
                {indexId: 4, value:'4月'},
                {indexId: 5, value:'5月'},
                {indexId: 6, value:'6月'},
                {indexId: 7, value:'7月'},
                {indexId: 8, value:'8月'},
                {indexId: 9, value:'9月'},
                {indexId: 10, value:'10月'},
                {indexId: 11, value:'11月'},
                {indexId: 12, value:'12月'},
            ],
            years: [], //根据当前年，前后8年区间
        },
        methods:{
            toDay:function(event){
                $('.item-border').removeClass('item-border-active');
                $(event.currentTarget).parent().find('.item-border').addClass('item-border-active');

                //调用接口显示当天的新币情况

                console.log(this.currYear);
                var data ={
                    date: this.currYear.toString()+addZero(this.currMinMonth).toString()+addZero(event.currentTarget.innerHTML),//	日期	是	8	String	yyyymmdd日期格式
                    element:  currencyList.optional,
                };
                getCoinNewDay(data);

            },
            //生成年份
            getYears: function(){
                var date = new Date();
                var currYear = date.getFullYear();
                var arr = [];
                var ra = -8;

                for(var i=currYear+ra; i<= currYear+Math.abs(ra); i++){
                    arr.push({indexId:i, value:i+'年'});
                    // ra++;
                }
                return arr;
            },
            //改变年份和月份
            setYearMonthDate: function(type,event){
                if(type == 'year'){
                    this.currYear = parseInt(event.currentTarget.value);
                    var month = this.currMaxMonth;
                }else{
                    var month = parseInt(event.currentTarget.value);
                }
                var firstDay = this.getFirstDay(this.currYear,month);
                if(firstDay == 0){
                    this.currMaxMonth = this.currMinMonth = month;
                }else{
                    this.currMaxMonth = month;
                    if(month-1 <= 0){
                        this.currMinMonth = 12;
                    }else{
                        this.currMinMonth = month-1;
                    }
                }
                var start = 1;
                for(var i=firstDay; i<7; i++){
                    $('.middle-week .item-value').eq(i).text(start);
                    start++;
                }

                if(month == 1){
                    var prevMonthDay = this.getMonthDays(this.currYear-1,12);
                }else{
                    var prevMonthDay = this.getMonthDays(this.currYear,month-1);
                }
                for(var i=firstDay-1; i>=0; i--){
                    $('.middle-week .item-value').eq(i).text(prevMonthDay);
                    prevMonthDay--;
                }
                this.setBorder(false);
                this.setToday();
            },
            //设置入参的日期
            setDayDate: function(day){
                var firstDay = this.getFirstDay(this.currYear,this.currMaxMonth);
                var maxDays = this.getMonthDays(this.currYear,this.currMaxMonth);
                //计算入参日期是周几
                var nowDay = (day + firstDay -1)% 7;
                if(nowDay == 0 ){
                    if(day+7 < maxDays ){
                        this.currMinMonth = this.currMaxMonth;
                    }else{
                        this.currMinMonth = this.currMaxMonth;
                        if(++this.currMaxMonth > 12){
                            this.currMinMonth = 1;
                        }
                    }
                }else{
                    if(day+(6-nowDay) > maxDays ){
                        this.currMinMonth = this.currMaxMonth;
                        if(++this.currMaxMonth > 12){
                            this.currMinMonth = 1;
                        }
                    }
                    if(day-nowDay <0){
                       this.currMinMonth = this.currMaxMonth -1;
                    }

                }
                var tempDayNum = 1; //临时天数，用处超过当前最大天数
                var tempDay = day;
                for(var i=nowDay; i< 7; i++){
                    if(tempDay>maxDays){
                        $('.middle-week .item-value').eq(i).text(tempDayNum);
                        tempDayNum++;
                    }else{
                        $('.middle-week .item-value').eq(i).text(tempDay);
                        tempDay++;
                    }
                }

                if(this.currMaxMonth == 1){
                    tempDayNum = this.getMonthDays(this.currYear-1,12);
                }else{
                    tempDayNum = this.getMonthDays(this.currYear,this.currMaxMonth-1);
                }
                tempDay =  day-1;
                for(var i=nowDay-1; i>=0; i--){
                    if(tempDay < 1){
                        $('.middle-week .item-value').eq(i).text(tempDayNum);
                        tempDayNum--;
                    }else{
                        $('.middle-week .item-value').eq(i).text(tempDay);
                        tempDay--;
                    }
                }

                // alert('Min:'+this.currMinMonth+' --- '+'Max:'+this.currMaxMonth);

                //改变字体颜色和边框
                // $('.middle-week .item-border').removeClass('item-border-active');
                // $('.middle-week .item-value').eq(nowDay).next().addClass('item-border-active');
                this.setBorder(nowDay);
                this.setToday();

            },
            setBorder: function(num){
                $('.middle-week .item-border').removeClass('item-border-active');
                if(num){
                    $('.middle-week .item-value').eq(num).next().addClass('item-border-active');
                }
            },
            setToday: function(){
                var day = this.isIntoWeek();
                $('.middle-week .item-value').removeClass('item-today');
                $('.middle-week .item-title').removeClass('item-today');
                if(day){
                    for(var i=0; i <7 ; i++){
                        if(parseInt($('.middle-week .item-value').eq(i).text()) == day ){
                            $('.middle-week .item-value').eq(i).addClass('item-today');
                            $('.middle-week .item-value').eq(i).prev().addClass('item-today');
                        }
                    }
                }
            },
            //往前 一周
            prevWeek:function(){
                var left = parseFloat($('#currency-nav .item-box').css('left'));
                $('.middle-week .item-border').removeClass('item-border-active');
                //设置 left-week 数据 和 middel-week数据
                var value = $('.middle-week .item-value').eq(0).text();
                value = parseInt(value); //第一个数据值

                // var isChange = false; //大小月同时修改

                for(var i=6; i>=0; i--){
                    if(value == 1 ){
                        if(i == 6){
                            //判断年和月减1的话，是否退一个
                            if(this.currMinMonth == 1){
                                this.currYear--;
                                this.currMaxMonth = this.currMinMonth = 12;
                            }else{
                                this.currMinMonth--;
                                this.currMaxMonth--;
                            }
                        }else{
                            //判断年和月减1的话，是否退一个
                            if(this.currMinMonth == 1){
                                this.currYear--;
                                this.currMinMonth = 12;
                            }else{
                                this.currMinMonth--;
                            }
                        }

                        value = this.getMonthDays(this.currYear,this.currMinMonth);
                        $('.left-week .item-value').eq(i).text(value);
                        $('.middle-week .item-value').eq(i).text(value);
                    }else{
                        if(i==6 && value-7 >0){
                            if(this.currMaxMonth != this.currMinMonth && this.currMaxMonth == 1){
                                this.currYear--;
                            }
                            this.currMaxMonth = this.currMinMonth;
                        }
                        value--;
                        // this.currMaxMonth = this.currMinMonth;
                        $('.left-week .item-value').eq(i).text(value);
                        $('.middle-week .item-value').eq(i).text(value);
                    }
                }

                $('#currency-nav .item-box').animate({'left':left+977},500,function(){
                    $('#currency-nav .item-box').css('left',-977);
                });
                this.setBorder(false);
                this.setToday();
            },
            //往后 一周
            nextWeek: function(){
                $('.middle-week .item-border').removeClass('item-border-active');
                var left = parseFloat($('#currency-nav .item-box').css('left'));
                //设置 left-week 数据 和 middel-week数据
                var value = $('.middle-week .item-value').eq(6).text();
                value = parseInt(value); //第一个数据值
                var days = this.getMonthDays(this.year,this.currMaxMonth);
                for(var i=0; i<7; i++){
                    if(value + 1 > days ){
                        if(i == 0 ){
                            //判断年和月减1的话，是否退一个
                            if(this.currMaxMonth+1 > 12){
                                ++this.currYear;
                                this.currMaxMonth = 1;
                                this.currMinMonth = 1;
                            }else{
                                this.currMaxMonth++;
                                this.currMinMonth++;
                            }
                            value = 1;
                        }else{
                            //判断年和月减1的话，是否退一个
                            if(this.currMaxMonth+1 > 12){
                                ++this.currYear;
                                this.currMaxMonth = 1;
                            }else{
                                this.currMaxMonth++;
                            }
                            value = 1;
                        }

                        $('.right-week .item-value').eq(i).text(value);
                        $('.middle-week .item-value').eq(i).text(value);
                    }else{
                        if(i==0 && value +7 <days ){
                            this.currMinMonth = this.currMaxMonth;
                        }
                        value++;
                        $('.right-week .item-value').eq(i).text(value);
                        $('.middle-week .item-value').eq(i).text(value);
                    }
                }

                var left = parseFloat($('#currency-nav .item-box').css('left'));
                //设置 right-week 数据 和  middel-week数据

                $('#currency-nav .item-box').animate({'left':left-977},500,function(){
                    $('#currency-nav .item-box').css('left',-977);
                });
                this.setBorder(false);
                this.setToday();
            },
            getMonthDays:function(year,month){
                year = parseInt(year);
                month = parseInt(month);
                var days = 0;
                switch(month){
                    case 2:
                        if(this.getRunNian(year)){
                            days = 29;
                        }else{
                            days =28;
                        }
                        break;
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        days = 31;
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        days = 30;
                        break;
                    default:
                        alert('错了');
                }
                return days;
            },
            //判断闰年
            getRunNian: function(year){
                if(year%400 ===0 ||(year%4===0 && year%100 !== 0)){
                    return true;
                }else{
                    return false;
                }
            },
            //判断某年某月的1号是星期几
            getFirstDay : function(year,month){
                var allDay = 0, y = year-1;
                allDay = y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + 1;
                for(var i=1; i< month ; i++ ){
                    switch(i){
                        case 2:
                            if(this.getRunNian(year)){
                                allDay += 29;
                            }else{
                                allDay +=28;
                            }
                            break;
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            allDay += 31;
                            break;
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            allDay += 30;
                            break;
                    }
                }
                return allDay%7;
            },
            //判断今天是否在当前的周历中
            isIntoWeek: function(){
                var date = new Date();
                var year = date.getFullYear();
                var month = date.getMonth()+1;
                var day = date.getDate();
                if(year != this.currYear){
                    return false;
                }
                if(month != this.currMinMonth && month != this.currMaxMonth){
                    return false;
                }
                if(day >15 && month != this.currMinMonth){
                    return false;
                }
                if(day <15 && month != this.currMaxMonth){
                    return false;
                }
                for(var i=0; i <7 ; i++){
                    if(parseInt($('.middle-week .item-value').eq(i).text()) == day ){
                        return day;
                    }
                }
                return false;
            },
        },
        mounted: function(){
            this.$nextTick(function(){
                // this.isIntoWeek();
                this.$nextTick(function(){
                    //获取入参
                    var args = getUrlParams();
                    if(args.year){
                        this.currYear = parseInt(args.year);
                        this.currMaxMonth = this.currMinMonth =  parseInt(args.month);
                        this.setDayDate(parseInt(args.day));
                    }else{
                        var d = new Date();
                        this.currYear = d.getFullYear();
                        this.currMaxMonth = this.currMaxMonth = d.getMonth()+1;
                        this.setDayDate(d.getDate());
                    }

                    this.years = this.getYears();
                });
            });
        }
    });


    //币种列表
    var currencyList = new Vue({
        el:'#currency-list',
        data:{
            //自选
            optional:{
                totalNum: 3,
                sortType: "", //排序类型
                sortWord:"", //排序字段
                // pageSize:"", //页面行数
                // pageNum: "", //当前页码
                list:[],
            },
        },
        methods:{
            //搜索
            toSearch: function(event){
                if(event.currentTarget.type == 'text'){
                    alert('开始搜索');
                    var str =  event.currentTarget.value;
                }else{
                    var str = $(event.currentTarget).prev().val();
                }
                if(str =='' || str == null || str == undefined){
                    $('.filtrate-input').focus();
                }else{
                    alert('开始搜索');
                }
            },
            //设置自选
            setFavorite:function(name,event){
                if(isLogin()){
                    var id = event.currentTarget.parentNode.parentNode.getAttribute('value');
                    var followId = event.currentTarget.parentNode.parentNode.getAttribute('followid');
                    var index = event.currentTarget.parentNode.getAttribute('value');
                    if(this[name].list[index].isFavorite){
                        //取消自选
                        removeSelfChange({followId: followId,element:this.optional,index:index});
                    }else{
                        //添加自选
                        addSelfChange({coinId: id , focusType:0,element:this.optional,index:index});
                    }
                }else{
                    location.href = 'login.html';
                }

            },
            changeSort: function(event){
                var currList = this.getCurrType()=='0' ? 'market': 'optional' ;  // 0 货币列表   1 自选
                // var list = [currencyList.market,currencyList.optional];
                var sortWord = event.currentTarget.getAttribute('type');  //按什么字段排序
                var sortType = event.currentTarget.getAttribute('value'); //降序1 升序 0

                this[currList].sortWord = sortWord;
                this[currList].pageNum = 1;
                if(sortType == '1'){
                    //降序排列
                    $('.sort-img').attr({value:1,src:'image/paixu1.png'});
                    this[currList].sortType = 0;
                    event.currentTarget.setAttribute('value','0');
                    event.currentTarget.setAttribute('src','image/paixu2.png');
                }else{
                    //升序排列
                    $('.sort-img').attr({value:1,src:'image/paixu1.png'});
                    this[currList].sortType = 1;
                    event.currentTarget.setAttribute('value','1');
                    event.currentTarget.setAttribute('src','image/paixu1.png');
                }

                //调用接口显示当天的新币情况
                var data ={
                    date: date,//	日期	是	8	String	yyyymmdd日期格式
                    element:  this.optional,
                };
                getCoinNewDay(data);


            },
            //跳转货币详细页
            goCurrencyProfile: function(event){
                var id = event.currentTarget.parentNode.getAttribute('value');
                // location.href = 'currencyProfile.html?id='+id;
                window.open( 'currencyProfile.html?id='+id);
            },
        },
        mounted:function(){
            this.$nextTick(function(){
                var args = getUrlParams();
                var date = args.year+addZero(args.month)+addZero(args.day);
                //调用接口显示当天的新币情况
                var data ={
                    date: date,//	日期	是	8	String	yyyymmdd日期格式
                    element:  this.optional,
                };
                getCoinNewDay(data);
            });
        },
    });


    //左右箭头,滑动函数
    $('.next-btn').on('click',{sign:'next'},blockSilder);
    $('.prev-btn').on('click',{sign:'prev'},blockSilder);

    //滑动函数，e为传入参数
    function blockSilder(e){
        var box = $('#currency-nav .item-box');
        var boxWidth = parseFloat(box.outerWidth());
        var group = $('#currency-nav .item-group');
        var item = $('.currency-item').eq(0);
        var itemWidth = parseFloat(item.outerWidth(true));
        if(e.data.sign == 'next'){
            var left = parseFloat(box.css('left')) - itemWidth-1;
            if(Math.abs(left) + group.width() <= boxWidth){
                $('#currency-nav .item-box').animate({left: left},300);
            }
        }else{
            var left = parseFloat(box.css('left')) + itemWidth+1;
            if(left <=0){
            $('#currency-nav .item-box').animate({left: left},300);
            }
        }
    }

    //计算滚动内容的宽度；
    function setSliderWidth() {
        var itemWidth = $('#currency-nav .currency-item').eq(0).outerWidth(); //获取每个item宽度
        $('#currency-nav').find('.item-box').eq(0).width(currHeader.currencyBlock.length * itemWidth + currHeader.currencyBlock.length);
    }
});